/*
	       File: ConfigurationManager.cs
	Create Date: 2007-07-31
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public class ConfigurationManager
    {
        #region Private member fields
        #endregion

        #region Constructors and cleanup methods
        private ConfigurationManager()
        {
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class attributes
        #endregion

        #region Events and delegates
        #endregion

        #region Public interface
        #region FileProcessor specific configuration stuff
        public static GuerillaProgrammer.FileProcessor.Settings.Configuration ReadConfiguration(string filePath)
        {
            try
            {
                if (true == File.Exists(filePath))
                {
                    StreamReader reader = new StreamReader(filePath);
                    XmlSerializer serializer = new XmlSerializer(typeof(GuerillaProgrammer.FileProcessor.Settings.Configuration));
                    GuerillaProgrammer.FileProcessor.Settings.Configuration config = (GuerillaProgrammer.FileProcessor.Settings.Configuration)serializer.Deserialize(reader);
                    reader.Close();

                    return config;
                }
                else
                {
                    Logger.LogError("File Does not exist", "The config file does not exist.  Requested file: {0}", filePath);
                    return null;
                }
            }
            catch (Exception exc)
            {
                Logger.LogError("Config Error", "An error occured reading the configuration file.  Error: {0}", exc.Message);
                return null;
            }
        }
        public static ConfigurationDirectory GetDirectoryByID(GuerillaProgrammer.FileProcessor.Settings.Configuration config, string id)
        {
            foreach (ConfigurationDirectory directory in config.Directories)
            {
                if (id == directory.ID)
                {
                    return directory;
                }
            }
            return null;
        }
        public static ConfigurationFileType GetFileTypeByID(GuerillaProgrammer.FileProcessor.Settings.Configuration config, string id)
        {
            foreach (ConfigurationFileType fileType in config.FileTypes)
            {
                if (id == fileType.ID)
                {
                    return fileType;
                }
            }
            return null;
        }
        public static ConfigurationWorkflow GetWorkflowByID(GuerillaProgrammer.FileProcessor.Settings.Configuration config, string id)
        {
            foreach (ConfigurationWorkflow workFlow in config.Workflows)
            {
                if (id == workFlow.ID)
                {
                    return workFlow;
                }
            }
            return null;
        }
        public static void SaveConfiguration(GuerillaProgrammer.FileProcessor.Settings.Configuration configuration, string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(GuerillaProgrammer.FileProcessor.Settings.Configuration));
            if (true == File.Exists(fileName))
            {
                File.Copy(fileName, Path.GetFileNameWithoutExtension(fileName) + ".backup");
                File.Delete(fileName);
            }
            StreamWriter writer = new StreamWriter(fileName);

            serializer.Serialize(writer, configuration);
        }
        #endregion

        #region Workflow Designer configuration
        public static WorkflowDesignerConfiguration ReadWorkflowConfiguration(string filePath)
        {
            try
            {
                if (true == File.Exists(filePath))
                {
                    StreamReader reader = new StreamReader(filePath);
                    XmlSerializer serializer = new XmlSerializer(typeof(WorkflowDesignerConfiguration));
                    
                    WorkflowDesignerConfiguration config = (WorkflowDesignerConfiguration)serializer.Deserialize(reader);
                    reader.Close();

                    return config;
                }
                else
                {
                    Logger.LogError("File Does not exist", "The config file does not exist.  Requested file: {0}", filePath);
                    return null;
                }
            }
            catch (Exception exc)
            {
                Logger.LogError("Config Error", "An error occured reading the configuration file.  Error: {0}", exc.Message);
                return null;
            }
        }
        public static string GetXOMLDirectory(WorkflowDesignerConfiguration configuration)
        {
            if (true == string.IsNullOrEmpty(configuration.WorkflowOutputDirectory))
            {
                return Environment.CurrentDirectory;
            }

            return configuration.WorkflowOutputDirectory;
        }
        public static string GetAssemblyDirectory(WorkflowDesignerConfiguration configuration)
        {
            if (true == string.IsNullOrEmpty(configuration.AssemblyOutputDirectory))
            {
                return Environment.CurrentDirectory;
            }

            return configuration.AssemblyOutputDirectory;
        }
        public static void AddLibraryDirectories(StringCollection libraryCollection, WorkflowDesignerConfiguration configuration)
        {
            foreach (string directory in configuration.Libraries)
            {
                libraryCollection.Add(directory);
            }
        }
        public static void AddReferencedAssemblies(StringCollection assemblyCollection, WorkflowDesignerConfiguration configuration)
        {
            foreach (string assembly in configuration.ReferencedAssemblies)
            {
                assemblyCollection.Add(assembly);
            }
        }
        #endregion
        #endregion
    }
}
